package org.mozhu.mboot.admin.config;

import org.mozhu.mboot.admin.config.session.JDBCSessionConfiguration;
import org.mozhu.mboot.admin.config.session.RedisSessionConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.session.web.http.CookieSerializer;
import org.springframework.session.web.http.DefaultCookieSerializer;

@Configuration
@Import({JDBCSessionConfiguration.class, RedisSessionConfiguration.class})
public class SessionConfiguration {

    /**
     * 将Spring Session默认的Cookie Key从SESSION替换为原生的JSESSIONID。
     * 而CookiePath设置为根路径且配置了相关的正则表达式，可以达到同父域下的单点登录的效果
     */
    @Bean
    public CookieSerializer cookieSerializer() {
        DefaultCookieSerializer serializer = new DefaultCookieSerializer();
        serializer.setCookieName("JSESSIONID");
        serializer.setCookiePath("/");
        serializer.setDomainNamePattern("^.+?\\.(\\w+\\.[a-z]+)$");
        return serializer;
    }
}
